package com.gexiaobao.pigeon.ui.fragment.setting

import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.util.CacheUtil
import com.gexiaobao.pigeon.app.util.DeviceUtil
import com.gexiaobao.pigeon.app.util.KvUtils
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentSettingBinding
import com.gexiaobao.pigeon.ui.activity.ActivityPrivacyAndService
import com.gexiaobao.pigeon.ui.activity.LoginActivity
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.SettingViewModel
import me.hgj.jetpackmvvm.ext.lifecycle.KtxActivityManger
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity


/**
 * @Author      : hxw
 * @Date        : 2023/5/30 8:31
 * @Describe    : 设置
 */
class FragmentSetting : BaseFragment<SettingViewModel, FragmentSettingBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.includeBar.toolbar.initClose(getString(R.string.mine_fragment_setting)) {
            nav().navigateUp()
        }

        mViewModel.versionName.set(getString(R.string.current_version) + "：" + activity?.let { DeviceUtil.getVerName(it) })
    }

    override fun initData() {
        super.initData()
        mViewModel.versionName.set(getString(R.string.current_version) + "：" + activity?.let { DeviceUtil.getVerName(it) })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.logOutResult.observe(viewLifecycleOwner) {
            dismissLoading()
            if (it.isSuccess) {
                CacheUtil.setIsLogin(false)
                CacheUtil.setUser(null)
                CacheUtil.setToken("")
                KvUtils.encode("pigeonMasterName", "")
                //清除保存的发送接鸽订单信息
                KvUtils.encode("drippingName", "")
                KvUtils.encode("drippingMobile", "")
                KvUtils.encode("drippingAddress", "")
                CacheUtil.setDrippingPigeonList(null)
                //清除地址信息
                CacheUtil.setAddressInfo(null)
                CacheUtil.setFirstAddress(true)//退出之后更新状态为true
                KtxActivityManger.finishAllActivity()
                RxToast.showToast(getString(R.string.Logged_out))
                startActivity<LoginActivity>()
                //带点渐变动画
                activity?.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }

        mViewModel.deleteAccountResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                CacheUtil.setIsLogin(false)
                startActivity<LoginActivity>()
                activity?.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                KtxActivityManger.finishAllActivity()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.mineSettingAbout,
            mDatabind.mineSettingAgreement,
            mDatabind.mineSettingPrivacy,
            mDatabind.mineSettingGesturePwd,
            mDatabind.mineSettingReceiveAddress,
            mDatabind.mineSettingPersonInfo,
            mDatabind.mineSettingLogout,
            mDatabind.mineSettingCancellation,
            mDatabind.mineSettingRecommendationManagement,
            mDatabind.mineSettingContactUs,
        ) {
            when (it) {
                mDatabind.mineSettingAbout -> {
                    nav().navigateAction(R.id.action_to_about)
                }

                mDatabind.mineSettingAgreement -> {
                    startActivity<ActivityPrivacyAndService>("type" to "2")
                }

                mDatabind.mineSettingPrivacy -> {
                    startActivity<ActivityPrivacyAndService>("type" to "1")
                }

                mDatabind.mineSettingGesturePwd -> {
                    nav().navigateAction(R.id.action_to_gesture_pwd)
                }

                mDatabind.mineSettingReceiveAddress -> {
                    nav().navigateAction(R.id.action_to_receive_address, Bundle().apply {
                        putInt("type", 0)
                    })
                }

                mDatabind.mineSettingPersonInfo -> {
                    nav().navigateAction(R.id.action_to_personal_info)
                }

                mDatabind.mineSettingLogout -> {
                    showLogOutDialog()
                }

                mDatabind.mineSettingCancellation -> {
                    accountCancellation()
                }

                mDatabind.mineSettingRecommendationManagement -> {
                    nav().navigateAction(R.id.action_to_recommendation_management)
                }

                mDatabind.mineSettingContactUs -> {
                    nav().navigateAction(R.id.action_to_fragment_contact_us)
                }
            }
        }
    }

    private fun showLogOutDialog() {
        val dialog = RxDialogDefault(context)
        dialog.setTitle(getString(R.string.is_sure_logged_out))
        dialog.setSureListener {
            dialog.dismiss()
            showLoading(getString(R.string.exiting))
            mViewModel.loginOut()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun accountCancellation() {
        val dialog = RxDialogDefault(context)
        dialog.setTitle(getString(R.string.Are_you_sure_you_want_to_close_your_account))
        dialog.setSureListener {
            mViewModel.accountCancellation()
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

}